package stark.app.function.imgedit;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.TitleBar;
import com.stark.imgedit.model.PhotoFrame;
import com.stark.imgedit.model.PhotoFrameInfo;

import java.util.List;
import java.util.concurrent.ExecutionException;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.app.R;
import stark.app.databinding.ActivityPhotoFrameDemoBinding;
import stark.common.basic.base.BaseTitleBarActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.AssetUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

/**
 * Author：daizhongbin on 2021/5/28
 * Email：1027154338@qq.com
 * Desc:
 */
public class PhotoFrameDemoActivity extends BaseTitleBarActivity<ActivityPhotoFrameDemoBinding> {

    public static void start(Context context, String imgPath) {
        Intent intent = IntentUtil.getIntent(context, PhotoFrameDemoActivity.class);
        intent.putExtra(Extra.PATH, imgPath);
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_photo_frame_demo;
    }

    @Override
    protected void initView() {
        super.initView();
        Intent intent = getIntent();
        String imgPath = intent.getStringExtra(Extra.PATH);
        if (TextUtils.isEmpty(imgPath)) {
            finish();
            return;
        }
        Glide.with(PhotoFrameDemoActivity.this).load(imgPath).into(mDataBinding.photoFrameView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.recyclerView.setLayoutManager(layoutManager);
        //adapter
        PhotoFrameAdapter frameAdapter = new PhotoFrameAdapter();
        frameAdapter.setNewInstance(loadData());
        frameAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                frameAdapter.setSelPos(position);
                setPhotoFrame(frameAdapter.getItem(position));
            }
        });
        mDataBinding.recyclerView.setAdapter(frameAdapter);
    }

    @NonNull
    @Override
    protected TitleBar getTitleBar() {
        return mDataBinding.titleBar;
    }

    @Override
    protected void initData() {
    }

    private List<PhotoFrame> loadData() {
        String jsonStr = AssetUtil.readFile2Str("photoFrame/photoFrame.json");
        List<PhotoFrame> list = GsonUtils.fromJson(jsonStr, new TypeToken<List<PhotoFrame>>() {
        }.getType());
        //添加不设置相框到头部
        list.add(0, new PhotoFrame());
        return list;
    }

    private void setPhotoFrame(PhotoFrame photoFrame) {
        final PhotoFrameInfo info = photoFrame.getInfo();
        if (info == null) {
            mDataBinding.photoFrameView.clearPhotoFrame();
            return;
        }
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                String filePath = AssetUtil.getFilePath("photoFrame", photoFrame.getResName());
                Bitmap bitmap = null;
                try {
                    bitmap = Glide.with(PhotoFrameDemoActivity.this).asBitmap().load(filePath).submit().get();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                emitter.onNext(bitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                mDataBinding.photoFrameView.setPhotoFrame(bitmap, info.getWidth(), info.getHeight(), info.getImgRect(),
                        info.getImgAngle());
            }
        });
    }
}
